package com.wanmait.myhouse.mediatorController;
import com.wanmait.myhouse.dao.*;
import com.wanmait.myhouse.vo.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("mediator")
public class MediatorAddHouseController {//添加房子的controller

    @PostMapping("selectType")
    @ResponseBody
    public List<SellManner> selectType(String type , Model model){
        List<SellManner> sellMannerList = null;
        if ("别墅".equals(type)){
            SellMannerDAO sellMannerDAO = new SellMannerDAO();
            sellMannerList = sellMannerDAO.selectAll();
            return sellMannerList;
        }else {
            sellMannerList = new ArrayList<>();
            SellManner sellManner = new SellManner();
            sellManner.setId(1);
            sellManner.setSellManner("出租");
            sellMannerList.add(sellManner);
            return sellMannerList;
        }
    }

    @PostMapping("AddHouseWrite")
    public String AddHouseWrite(House house, MultipartFile img,HttpServletRequest request){
        if (!img.isEmpty()){
            String filepath = request.getServletContext().getRealPath("//static/uploadImages");
            String originalFilename = img.getOriginalFilename();
            String fileext = FilenameUtils.getExtension(originalFilename);
            String filename = UUID.randomUUID().toString() + "." + fileext;
            try {
                house.setShowImg(filename);
                img.transferTo(new File(filepath+"/"+filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        HouseDAO houseDAO = new HouseDAO();
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Timestamp time = Timestamp.valueOf(localDateTime.format(formatter));
        house.setTime(time);
        houseDAO.AddHouse(house);
        return "redirect:/mediator/showHouse";
    }

    @GetMapping("AddHouse")
    public String AddHouse(HttpServletRequest request){//获取所有要用到的city，type，room，将来要通过select选择
        CityDaO cityDao = new CityDaO();
        List<City>cityList = cityDao.FindAll();
        request.setAttribute("cityList",cityList);
        TypeDAO typeDAO = new TypeDAO();
        List<Type>typeList = typeDAO.FindAll();
        request.setAttribute("typeList",typeList);
        RoomDAO roomDAO = new RoomDAO();
        List<Room>roomList = roomDAO.FindAll();
        request.setAttribute("roomList",roomList);
        return "/mediator/addhouse";
    }
}
